We will need to know a little bit about Python in order for the following tutorial to make sense. This will not be a comprehensive introduction to Python. It will be just enough to get us started.
Many people are still using Python 2.7. We are using 3 because it is easier to use to study Chinese language. Python 2.7 is no longer being updated (aside from security updates).
A is not the same thing as a.
You will need to use quotation marks throughout your code. Be careful, if you type commas or quotation marks while typing in Chinese, Python will not know how to handle them.
This allows you to tell someone who is reading your code what it does.
A variable can be thought of as a way to contain information. You have to create them in order to use them. They can store any type of information in Python. We can call them ALMOST anything we want. We should avoid reserved words in Python (if, or, file, etc.). It should start with a lowercase letter and not an uppercase one or a number.
Variables allow us to save information to reuse it later.
In [2]:
# You can store integers
x = 10
# You can store strings
y = "Hi, my name is Paul"
# A variable can be as long as you like. It is best to use variable names
# that express what the variable is.
long_variable_names_work_too = 1.3
hi = 'hello'
In [4]:
print("It will change")
In [3]:
# Here are some integers:
2
5
5000
Out[3]:
In [4]:
# Here is some regular division:
5/2
Out[4]:
In [5]:
# Here is some integer division:
5//2
Out[5]:
In [6]:
# Here are some floating point numbers:
1.4
200.12
.008
Out[6]:
In [7]:
# Here is some floating point number division:
1000.15/13
Out[7]:
Note the trailing numbers. They are not extremely precise. Be careful
In [8]:
"This is a string."
'This is also a string.'
Out[8]:
In [9]:
my_string = "This is my string."
print(my_string[0])
print(my_string[11:15])
print(my_string[-4:])
In [10]:
print(1<5)
print(2>5)
print(4==4)
In [11]:
# This is an empty list:
[]
# This is a list with some information.
[1, 2, 3, 4, 5, 6]
Out[11]:
In [12]:
numbers = [1,2,3,4,5,6]
print(numbers[0])
In [13]:
# This is an empty list:
{}
# This is a list with some information:
{"Independence Day":"July 4th", "Halloween":"October 31st", "Labor Day 2016":"September 6th"}
Out[13]:
In [14]:
holiday_dates = {"Independence Day":"July 4th", "Halloween":"October 31st", "Labor Day 2016":"September 6th"}
print(holiday_dates["Halloween"])
In [15]:
i = 0
while i < 4:
print(i)
# Increase i by one. This can also be written i += 1
i = i + 1
In [16]:
animals = ["tiger", "lion", "monkey", "pig"]
for animal in animals:
print(animal)
We can use code other people have written by importing libraries. These extend the basic functionality of Python. They are not automatically imported into the namespace for efficiency reasons. Anaconda comes with many libraries that will make our life much easier. We will import them as needed
In [17]:
import math, os, re
In [ ]: